#include "data_proc.h"
#include <openssl/bio.h>  
#include <openssl/err.h>  
#include <openssl/ssl.h>  
#include <openssl/aes.h>
#include <stdio.h>
#include <string.h>

#include <pthread.h>
#include <sys/select.h>
#include <sys/time.h>
#include <unistd.h>

extern int tarNSecond;
extern pthread_mutex_t mutex_uart4;

int isSendingImage = 0;

void aes128_enc_64bytes(const unsigned char * userKey,const unsigned char * dataIn,unsigned char * dataOut)
{
    AES_KEY encKey;
    int i = 0;
    const unsigned char * ptrDataIn = dataIn;
    unsigned char * ptrDataOut = dataOut;
    int setKeyRet = AES_set_encrypt_key(userKey, 128, &encKey);
    if(setKeyRet<0)
    {
        printf("failed to set key for encryption!\n");
        return;
    }

    for(i=0;i<4;i++)
    {
       AES_encrypt(ptrDataIn, ptrDataOut,&encKey);
       ptrDataIn += 16;
       ptrDataOut += 16;
    }   
}

void getUserKey(unsigned char * usrKeyOut)
{
    memset(usrKeyOut,0,16);
}


void formatHexStr(char * buff, int sizeRecv,char * buffOut)
{
	int i=0;
	memset(buffOut,0,1024);
	for(i=0;i<(sizeRecv-1);i++)
	{
		sprintf(buffOut,"%s%02x ",buffOut,buff[i]&0x00ff);
	}
	sprintf(buffOut,"%s%02x\n",buffOut,buff[i]&0x00ff);
}
//indexTerm: the terminator's number
void enc_loc_pac(char*buffOut,char * encodedLoc,int sizeIn, unsigned short indexTerm)
{
    unsigned short checkSum = 0;
    unsigned char * data = (unsigned char *)buffOut;
    int i=0;
    buffOut[0] = 0x24;
    buffOut[1] = indexTerm&0x00ff;
    buffOut[2] = (indexTerm>>8)&0x00ff;

    memcpy(buffOut+3,encodedLoc,sizeIn);

    for(i=0;i<67;i++)
    {
       checkSum += *data;
       data ++ ;
    }
    buffOut[67] = checkSum&0x00ff;
    buffOut[68] = (checkSum>>8)&0x00ff;
    
    buffOut[69] = 0x0d;
    buffOut[70] = 0x0a;
}

int get_cap_freq(double curSeed)
{
    int retVal = 30*60;//30 minutes
    if(curSeed<=0.0)
    {
        retVal = tarNSecond;
    }
	else if(curSeed<=20.0)
	{
	    retVal = 10*60;//get image every 10 minutes 
	}
	else if(curSeed<=80.0)
	{
	    retVal = 5*60;//5 minutes
	}
	else
	{
	    retVal = tarNSecond;
	}
	return retVal;
}

void sendLocationPack(int fd, char * buff, int len)
{
   int sendCount = 0;
   if(fd==-1)
   {
       printf("fd for uart4 is not normal\n");
       return;
   }

   if(isSendingImage == 0)
   {
       pthread_mutex_lock(&mutex_uart4);
       sendCount = write(fd,buff,len);
	   if(sendCount != len)
	   {
		    printf("send encypted location pack failed\n");
	   }   
       pthread_mutex_unlock(&mutex_uart4);
   }
   
}

void sendImagePack(int fd, char * buff, int len)
{
    int remain = len;
	int sendCount = 0;
	char * ptrBuff = buff;
	int ret = 0;

    fd_set fds;
	struct timeval tv;


	FD_ZERO(&fds);   
    tv.tv_sec = 0;
    tv.tv_usec = 200;
   
	isSendingImage = 1;	
	while(remain)
	{
	   FD_SET(fd,&fds);

	   ret = select(fd+1,NULL,&fds,NULL,&tv);
	   if(ret < 0)
	   {
	      printf("Fail to select!\n");
          continue;
	   }

	   if(FD_ISSET(fd, &fds))
	   {
	       if(remain>512)
		   {
		       pthread_mutex_lock(&mutex_uart4);
		       sendCount = write(fd,ptrBuff,512);
			   pthread_mutex_unlock(&mutex_uart4);
			   ptrBuff += sendCount;

			   remain -=  sendCount;
		   }
		   else
		   {
		       pthread_mutex_lock(&mutex_uart4);
		       sendCount = write(fd,ptrBuff,remain);
			   pthread_mutex_unlock(&mutex_uart4);
			   ptrBuff += sendCount;

			   remain -=  sendCount;
		   } 
	   }	

	   usleep(200);	   
	}

	//if(sendCount != len)
   // {
	//    printf("send encypted location pack failed\n");
	//} 
	
	isSendingImage = 0;
}


